from django.test import TestCase
import base64
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5

prikey = None

def my_decrypt(encry_b64str):
    global prikey
    if not prikey:
        v15_key = get_my_v15_prikey()
        prikey = v15_key
    else:
        v15_key = prikey
    random_generator = Random.new().read
    text_bytes = v15_key.decrypt(base64.b64decode(encry_b64str),None)
    return text_bytes.decode()

def get_my_v15_prikey():
    with open("pri.pem",'rb') as f:
        print("read key")
        key = RSA.import_key(f.read())
        v15_key = PKCS1_v1_5.new(key)
        return v15_key
    return None

if __name__ == "__main__":
    jiamide = 'ahJJOApmdbFXIZX5H5iT+/HCD2o1JWOsHEAAb5W+Wy8qPbH6fTeYNa/eESgCzdDdnPjXxhEhAja4ChjnlSbwQZCuxCfmL2OirdoQWMRKARLUInKXTro2nD5RZHfvIqM1wTQbDpEsilZpv0zmPwnRM8/9Q2O5k07aYzntmbGBjJU='
    print(my_decrypt(jiamide))
